package service

import (
	"myserver/dao"
	"myserver/model"
	"myserver/param"
	"myserver/tool"
)

type ReservationService struct {
}

func (s *ReservationService) QueryReservation(query string) model.Reservation {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	return restDao.QueryReservation(query)
}

func (s *ReservationService) CreateReservation(reservation model.Reservation) int64 {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	res := restDao.CreateReservation(reservation)
	return res
}

func (s *ReservationService) DeleteReservation(reservation model.Reservation) int64 {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	return restDao.DeleteReservation(reservation)
}

func (s *ReservationService) UpdateReservation(reservation model.Reservation) int64 {
	if reservation.ResourceType == 2 {
		return UpdateWorkReservation(reservation)
	} else {
		return UpdateSubsidyReservation(reservation)
	}
}
func UpdateWorkReservation(reservation model.Reservation) int64 {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	workService := WorkService{}
	work := model.Work{}
	work.ID = reservation.ResourceID
	amount := int64(workService.GetWork(work).Amount)
	rsAmount := workService.GetRsWork(work)
	if amount > rsAmount {
		workService.CreateRsWork(reservation)
		return restDao.UpdateReservation(reservation)
	} else {
		return 0
	}
}
func UpdateSubsidyReservation(reservation model.Reservation) int64 {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	subsidyService := SubsidyService{}
	subsidy := model.Subsidy{}
	subsidy.ID = reservation.ResourceID
	amount := int64(subsidyService.GetSubsidy(subsidy).Amount)
	rsAmount := subsidyService.GetRsSubsidy(subsidy)
	if amount > rsAmount {
		subsidyService.CreateRsSubsidy(reservation)
		return restDao.UpdateReservation(reservation)
	} else {
		return 0
	}
}
func (s *ReservationService) QueryReservations(pageParam param.PageParam) param.ResponsePageParam {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	res := restDao.QueryReservations(tool.ToPageParam(pageParam))
	return res
}

func (s *ReservationService) CreateRsWork(pageParam param.PageParam) int64 {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	res := restDao.CreateRsWork(tool.ToPageParam(pageParam))
	return res
}

func (s *ReservationService) CreateRsSubsidy(pageParam param.PageParam) interface{} {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	res := restDao.CreateRsSubsidy(tool.ToPageParam(pageParam))
	return res
}

func (s *ReservationService) QueryUserReservations(pageParam param.PageParam) param.ResponsePageParam {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	res := restDao.QueryUserReservations(tool.ToPageParam(pageParam))
	return res
}

func (s *ReservationService) UpdateRefuseReservation(reservation model.Reservation) interface{} {
	restDao := dao.ReservationDao{MyOrm: tool.MyDb}
	return restDao.UpdateRefuseReservation(reservation)
}
